package com.example.springstudy.controller;

import com.example.springstudy.common.PagedResultDto;
import com.example.springstudy.common.SuccessResult;
import com.example.springstudy.dto.DataEventInput;
import com.example.springstudy.entity.Ins;
import com.example.springstudy.services.InsRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

// 数据报警控制器
@RestController
@RequestMapping("api/ins")
public class InsController {
    @Resource
    private InsRepository insRepository;

    @PostMapping
    public SuccessResult<Ins> addOne(@RequestBody Ins ins) {
        insRepository.save(ins);
        return new SuccessResult<>(ins);
    }


    @GetMapping("{id}")
    public SuccessResult<Ins> getOne(@PathVariable Long id) {
        Optional<Ins> byId = insRepository.findById(id);
        if (byId.isPresent()) {
            return new SuccessResult<>(byId.get());
        }
        throw new RuntimeException(" 巡 检 not found");
    }

    @GetMapping("")
    public SuccessResult<PagedResultDto<Ins>> getList(DataEventInput input) {
        List<Ins> list = (List<Ins>) insRepository.findAll();
        PagedResultDto<Ins> pageRes = new PagedResultDto<>();
        pageRes.setItems(list);
        pageRes.setTotal((long) list.size());
        return new SuccessResult<>(pageRes);
    }

}
